package net

import io.ktor.client.HttpClient
import io.ktor.client.network.sockets.ConnectTimeoutException
import io.ktor.client.plugins.HttpRequestRetry
import io.ktor.client.plugins.HttpRequestTimeoutException
import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.network.sockets.SocketTimeoutException

object Net {
    val client = HttpClient {
        install(HttpTimeout) {
            connectTimeoutMillis = 8_000
            socketTimeoutMillis = 10_000
            requestTimeoutMillis = 15_000
        }
        install(HttpRequestRetry) {
            maxRetries = 2
            retryIf { request, cause ->
                cause is ConnectTimeoutException ||
                        cause is HttpRequestTimeoutException ||
                        cause is SocketTimeoutException
            }
            constantDelay(300)
        }

    }

    suspend fun getStatus(url: String): Int {
        val resp: HttpResponse = client.get(url)
        return resp.status.value
    }
}